package com.lightboat.web.controller.crm;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.lightboat.common.annotation.Anonymous;
import com.lightboat.common.config.properties.OpenApiCredentialProperties;
import com.lightboat.common.core.controller.BaseController;
import com.lightboat.common.core.domain.AjaxResult;
import com.lightboat.common.core.domain.bo.Credential;
import com.lightboat.common.utils.appSigner.BaiduAppSigner;
import com.lightboat.system.service.ICrmAfterSaleService;
import com.lightboat.system.service.ICrmOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/listeningData")
@Tag(name = "监听数据")
@Anonymous
public class CrmListeningDataController extends BaseController {

    private final OpenApiCredentialProperties apiCredentialProperties;
    private final ICrmOrderService crmOrderService;
    private final ICrmAfterSaleService crmAfterSaleService;

    @Operation(summary = "监听百度订单")
    @PostMapping("/baiduOrder")
    public AjaxResult listeningBaiduOrder(@RequestBody String  dataStr, @RequestHeader(name = "sign") String sign, @RequestHeader(name = "appId") String appId){
        log.debug("监听百度数据，签名：{},应用ID：{}，数据:{}", sign, appId, dataStr);
        Credential baiduCredential = apiCredentialProperties.getBaidus().get(0);
        BaiduAppSigner baiduAppSigner = new BaiduAppSigner(baiduCredential.getAppKey(), baiduCredential.getSecretKey());
        if (!sign.equals(baiduAppSigner.callbackVerifySign(dataStr))) {
            return error("签名验证失败");
        }
        JSONObject jsonObject = JSON.parseObject(dataStr);

        /**
         * "0"	测试消息
         * "1"	订单相关消息
         * "2"	处方相关消息
         * "3"	商品相关消息
         * "4"	售后单相关消息
         */
        int type = jsonObject.getIntValue("type", 0);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONObject extra = jsonObject.getJSONObject("extra");
        int status = data.getIntValue("status", 0);
        switch (type) {
            case 0:
                // 测试消息
                log.info("测试消息：{}", data);
                break;
            case 1:
                // 订单相关消息
                crmOrderService.listeningOrder(data, extra, status);
                break;
            case 2:
                // 处方相关消息
                log.info("处方相关消息：{}", data);
                break;
            case 3:
                // 商品相关消息
                log.info("商品相关消息：{}", data);
                break;
            case 4:
                // 售后单相关消息
                log.info("售后单相关消息：{}", data);
                crmAfterSaleService.listeningAfterSale(data, extra, status);
                break;
            default:
                log.warn("未知消息类型：{}", type);
        }
        return success();
    }
}
